
#include "Util.h"
#include <math.h>

#include <fstream>

bool fileExists(const char* fileName)
{
  std::fstream fin;
  fin.open(fileName,std::ios::in);
  if( fin.is_open() )
  {
    fin.close();
    return true;
  }
  fin.close();
  return false;
}


//returns a value between 0 and ub (inclusive)
int intRand(int ub)
{
	return  rand() / (RAND_MAX / ub);
}


double floatRand(double ub)
{
	return  (double) (rand() / (double)(RAND_MAX / ub));
//	double result = floatRand(0.0,ub); 
//	printf("rnd: ub: %lf result: %lf\n", ub, result); 
//	return result; 
//	return drand48() * (ub);
}

double floatRand(double lb, double ub)
{
//	return drand48() * (ub-lb) + lb;
	return floatRand(ub-lb) + lb; 
}

double abs_double(double d)
{
	if (d>=0)
		return d; 
	else
		return -d; 
}


double deg2rad(double deg)
{
	return deg/57.3; 
}

double rad2deg(double rad)
{
	return rad*57.3;
}


#ifdef OS_WINDOWS
double drand48(){return floatRand(1.0);}
#endif


#ifndef OS_WINDOWS
timeval measure_time_end, measure_time_start; //used to measure time 

void measureTimeStart()
{
	gettimeofday(&measure_time_start,NULL); 
}

double measureTimeEnd()
{
	gettimeofday(&measure_time_end,NULL); 
	return (double) (measure_time_end.tv_sec - measure_time_start.tv_sec); 
}

#endif






